package com.boniu.startapp.services;

import android.app.Activity;
import android.app.ActivityManager;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Handler;
import android.os.Message;
import android.telephony.PhoneStateListener;
import android.telephony.TelephonyManager;
import android.util.Log;

import androidx.annotation.NonNull;

import com.boniu.startapp.Config;
import com.boniu.startapp.SPUtils;

import java.util.logging.LogRecord;

class LockScreenBroadcastReceiver extends BroadcastReceiver {
    public static final String TAG = LockScreenBroadcastReceiver.class.getSimpleName();
    private static LockScreenBroadcastReceiver mReceiver;
    public static void register(Context context) {
        if (mReceiver != null) {
            return;
        }
        mReceiver = new LockScreenBroadcastReceiver();
        IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(Intent.ACTION_SCREEN_ON);
        intentFilter.addAction(Intent.ACTION_SCREEN_OFF);
        intentFilter.setPriority(Integer.MAX_VALUE);
        context.registerReceiver(mReceiver, intentFilter);
    }


    @Override
    public void onReceive(Context context, Intent intent) {
        if (Intent.ACTION_SCREEN_OFF.equals(intent.getAction())) {
            MyAccessibilityService.mainFunctions.setLock(true);
            Log.e(TAG, "onReceive:1 " );

        }else if (Intent.ACTION_SCREEN_ON.equals(intent.getAction())){
            SPUtils.getInstance().put(Config.IS_LOCK,true);
            handler.sendEmptyMessageDelayed(0,8000);
            Log.e(TAG, "onReceive: 2" );
        }
    }
    private Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            MyAccessibilityService.mainFunctions.setLock(false);

        }
    };
}
